<?php

namespace app\index\model;

use think\Model;
use think\model\concern\SoftDelete;

class CommentModel extends Model
{
    use SoftDelete;
    protected $name = 'car_comment'; //表名      默认类型
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $dateFormat = 'Y/m/d H:i:s';
    protected $createTime = 'create_at';
    protected $updateTime = 'update_at';
    protected $deleteTime='delete_at';
    public function userInfo()
    {
        return $this->hasOne(UserModel::class, 'id', 'userId')->field(['id', 'avatar', 'rank', 'nickname', 'comment_bg_id']);
    }

    public function childer()
    {
        return $list = $this->hasMany(CommentReplyModel::class,  'commentId', 'id')->with(['userInfo','fatherUserInfo'])->limit(3)->order(['sort'=>'desc','create_at'=>'desc']);
    }

    public function reply() {
        return $this->hasOne(CommentReplyModel::class, 'commentId', 'id');
    }

    public function like() {
        return $this->hasOne(CommentLike::class, 'relevancyId', 'id')->where('type', 1);
    }

    public function likeNum() {
        return $this->hasOne(CommentLike::class, 'relevancyId', 'id')->where('type', 1);
    }

    public function resource()
    {
        return $this->hasOne(PhotoList::class, 'id', 'exchangeId')->bind([
            'photo_id' => 'id',
            'photo_name' => 'name',
            'cover'
        ]);
    }
}